#include <jni.h>
#include <string>
#include "native-lib.h"

extern "C" JNIEXPORT jbyteArray JNICALL
Java_com_example_jnidemo_MainActivity_AESCBCencryptbuffer(JNIEnv *env,
                                                          jclass obj,
                                                          jstring uid,
                                                          jbyteArray buff,
                                                          jint buffLen) {
    int32_t uidLen = env->GetStringUTFLength(uid);
    const char *tmpUid = env->GetStringUTFChars(uid, NULL);//Java String to C Style string

    uint8_t buffArr[buffLen];
    memset(buffArr, 0, sizeof(buffArr));
    env->GetByteArrayRegion(buff, 0, buffLen, (jbyte *)buffArr);

    //env->ReleaseStringUTFChars(buff, tmpBuff);
    env->ReleaseStringUTFChars(uid, tmpUid);

	const char* presult = encrypt_buffer(pKey, tmpUid, uidLen, (const char *)buffArr, buffLen);
    jbyteArray jarrRV = env->NewByteArray(buffLen);
    env->SetByteArrayRegion(jarrRV, 0, buffLen, (const jbyte*) buffArr);
	delete[] presult;
    return jarrRV;
}

extern "C" JNIEXPORT jbyteArray JNICALL
Java_com_example_jnidemo_MainActivity_AESCBCdecryptbuffer(
        JNIEnv *env,
        jclass obj,
        jstring uid,
        jbyteArray buff,
        jint buffLen) {
    int32_t uidLen = env->GetStringUTFLength(uid);
    const char *tmpUid = env->GetStringUTFChars(uid, NULL);//Java String to C Style string

    uint8_t buffArr[buffLen];
    memset(buffArr, 0, sizeof(buffArr));
    env->GetByteArrayRegion(buff, 0, buffLen, (jbyte*)buffArr);

    //env->ReleaseStringUTFChars(buff, tmpBuff);
    env->ReleaseStringUTFChars(uid, tmpUid);

    const char* presult = decrypt_buffer(pKey, tmpUid, uidLen, (const char *)buffArr, buffLen);
    jbyteArray jarrRV = env->NewByteArray(AES_BLOCKLEN);
    env->SetByteArrayRegion(jarrRV, 0, AES_BLOCKLEN, (const jbyte*) presult);
	delete[] presult;
    printf("---", jarrRV);
    return jarrRV;
}